"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : DATABASE
Case Name   : 关闭增量检查点,开启全页写,无负载情况下,create database
Create At   : 2023/02/15
Owner       : @kyeleze
Description :
    1.关闭增量检查点，打开全页写，无负载,重启数据库
    2.创建database,记录创建database时间
Expect      :
    1.成功
    2.databases创建时间不超过5秒
History     :
"""

import os
import unittest
import datetime

from yat.test import macro
from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class DdlDatabaseCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.primary_user_sh = Node(node='PrimaryDbUser')
        self.constant = Constant()
        self.primary_com = Common()
        self.database_name = "t_ddl_test_022"
        self.incremental_checkpoint = \
            self.primary_com.show_param("enable_incremental_checkpoint")
        self.full_page_writes = self.primary_com.show_param("full_page_writes")
        self.double_write = self.primary_com.show_param("enable_double_write")

    def test_database(self):
        text = '--step1:关闭增量检查点，打开全页写，无负载,重启数据库 expect:成功--'
        self.log.info(text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_incremental_checkpoint=off')
        self.assertTrue(status, '执行失败:' + text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'full_page_writes=on')
        self.assertTrue(status, '执行失败:' + text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            'enable_double_write=off')
        self.assertTrue(status, '执行失败:' + text)
        result = self.primary_sh.restart_db_cluster(True)
        flg = self.constant.START_SUCCESS_MSG in result \
              or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)

        text = '--step2:创建database,记录创建database时间 ' \
               'expect:databases创建时间不超过5秒--'
        self.log.info(text)
        start_time = datetime.datetime.now()
        sql_result = self.primary_sh.execut_db_sql(f'drop database if exists '
                                                   f'{self.database_name};'
                                                   f'create database '
                                                   f'{self.database_name};')
        end_time = datetime.datetime.now()
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)
        all_time = (end_time - start_time).total_seconds()
        self.log.info(all_time)
        self.assertLess(all_time, 5, '执行失败:' + text)

    def tearDown(self):
        text = '--清理环境--'
        self.log.info(text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_incremental_checkpoint={self.incremental_checkpoint}')
        self.assertTrue(status, '执行失败:' + text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'full_page_writes={self.full_page_writes}')
        self.assertTrue(status, '执行失败:' + text)
        status = self.primary_sh.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            f'enable_double_write={self.double_write}')
        self.assertTrue(status, '执行失败:' + text)
        result = self.primary_sh.restart_db_cluster(True)
        flg = self.constant.START_SUCCESS_MSG in result \
              or 'Degrade' in result
        self.assertTrue(flg, '执行失败:' + text)
        sql_result = self.primary_sh.execut_db_sql(f'drop database '
                                                   f'{self.database_name};')
        self.log.info(sql_result)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_result,
                      '执行失败:' + text)
        self.log.info(f'----{os.path.basename(__file__)} end----')

